/**
 * Desenvolvido pela Fábrica de Software do CESUPA.
 * Todos os direitos reservados.
 */
package br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule;

import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.entities.HelpTopic;
import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.entities.SearchResult;
import br.cesupa.fabsoft.nomedoprojeto.utils.ApplicationTools;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Hit;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;

/**
 *
 * @author Breno Leite
 */
public class HelpSearcher {

    private File indexDirectory = null;
    private static final String DEFAULT_INDEXES_DIRECTORY = "help/help-meta-inf/indexes/";

    public HelpSearcher() {
        indexDirectory = new File(ApplicationTools.getDirectoryInApplication(
                DEFAULT_INDEXES_DIRECTORY));
    }

    public List<SearchResult> search(String searchQuery) {
        List<SearchResult> searchResults = new ArrayList<SearchResult>();
        if (indexDirectory.exists() && indexDirectory.listFiles().length != 0) {
            try {
                Query query = new MultiFieldQueryParser(new String[]{
                            "title", "keywords", "content"},
                        new StandardAnalyzer()).parse(searchQuery);

                IndexSearcher indexSearcher = new IndexSearcher(
                        indexDirectory.getPath());
                Hits hits = indexSearcher.search(query);

                for (Iterator it = hits.iterator(); it.hasNext();) {
                    Hit hit = (Hit) it.next();
                    SearchResult searchResult = new SearchResult();
                    HelpTopic relatedHelpTopic = new HelpTopic();

                    searchResult.setScore(hit.getScore());
                    relatedHelpTopic.setTitle(hit.get("title"));
                    relatedHelpTopic.setPath(hit.get("path"));
                    relatedHelpTopic.setKeywords(hit.get("keywords"));
                    relatedHelpTopic.setDescription(hit.get("description"));
                    searchResult.setRelatedHelpTopic(relatedHelpTopic);

                    searchResults.add(searchResult);
                }
                indexSearcher.close();
                Collections.sort(searchResults);
            } catch (CorruptIndexException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
        }
        return searchResults;
    }
}
